package cn.task.part2.part3.window;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

public class MainWindow extends JFrame implements ActionListener {
	JMenuBar menubar;
	JMenu menu;
	JMenuItem[] menuitems;
	JLabel label;
	JPanel panel;
	String[] menuitemsnames = { "录入Customer","录入Preferred"};
	CardLayout cardlayout;
	CustomerWindow customerWindow;
	PreferredWindow preferredWindow;

	public MainWindow() throws SQLException {
		init();
		this.setLocation(350, 150);
		setSize(800, 600);
		setVisible(true);
		setResizable(false);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}

	public void init() throws SQLException {
		menubar = new JMenuBar();
		menu = new JMenu("操作菜单");
		menu.setFont(new Font("Serif", Font.PLAIN, 22));
		menuitems = new JMenuItem[5];
		for (int i = 0; i < menuitemsnames.length; i++) {
			menuitems[i] = new JMenuItem(menuitemsnames[i]);
			menuitems[i].setFont(new Font("宋体", Font.PLAIN, 18));
			menuitems[i].addActionListener(this);// 注册监听器
			menu.add(menuitems[i]);
		}
		menubar.add(menu);
		setJMenuBar(menubar);
		panel = new JPanel();
		//必须设置为透明的。否则看不到图片
		panel.setOpaque(false);
		cardlayout = new CardLayout();
		label = new JLabel("part3Andpart4", JLabel.CENTER);
		label.setFont(new Font("隶书", Font.BOLD, 40));
		label.setForeground(Color.blue);
		panel.setLayout(cardlayout);
		panel.add("welcome", label);
		
		customerWindow = new CustomerWindow();
		panel.add("customer", customerWindow);
		
		preferredWindow = new PreferredWindow();
		panel.add("preferred", preferredWindow);

		panel.setBackground(Color.WHITE);
		add(panel, BorderLayout.CENTER);
	}

	public static void main(String[] args) throws SQLException {
		new MainWindow();
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getActionCommand().equals("录入Customer")) {
			cardlayout.show(panel, "customer");
		}else if(arg0.getActionCommand().equals("录入Preferred")) {
			cardlayout.show(panel, "preferred");
		}
	}

}
